﻿using SDATA;
using SDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SABI
{
    public partial class frm0050 : Form
    {
        bool _isNew = true;
        string sql = "";
        string _BankAccountID;

        public frm0050()
        {
            InitializeComponent();
        }

        public frm0050(bool isNew = true, bool isView = false, string BankAccountID = "")
        {
            InitializeComponent();
            _isNew = isNew;
            btnSave.Visible = !isView;
            txtBankAccountID.Enabled = isNew;
            btnAdd.Visible = isNew;
            _isNew = isNew;
            _BankAccountID = BankAccountID;
        }

        private void frm0050_Load(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = true;
            txtBankAccountID.Enabled = true;
            _isNew = true;
            foreach (Control c in this.Controls)
                if (c.GetType() == typeof(TextBox))
                    c.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) { return; }
            if (!_isNew)
            {
                UpdateInfo();
            }
            else
            {
                AddNew();
            }
        }

        private bool CheckInput()
        {
            if (txtBankAccountID.Text.Trim() == "")
            {
                DLL02.Alert("Bạn phải nhập Mã ngân hàng!");
                txtBankAccountID.Focus();
                return false;
            }
            else if (txtBankName.Text.Trim() == "")
            {
                DLL02.Alert("Bạn phải nhập Tên ngân hàng!");
                lblBankName.Focus();
                return false;
            }
            return true;
        }

        private void AddNew()
        {
            sql = "INSERT tb0034(BankAccountID,AccountID,CurrencyID,BankAccountNo,";
            sql += "BankName,Notes,Disabled,OpenDate,CloseDate,";
            sql += "CreateDate,CreateUserID,LastModifyDate,LastModifyUserID) VALUES(";
            sql += string.Format("'{0}','{1}','{2}',N'{3}',", txtBankAccountID.Text, TDBCboAccountID.Text, TDBCboCurrencyID.Text, txtBankAccountNo.Text);
            sql += string.Format("N'{0}',N'{1}','{2}','{3}','{4}',", txtBankName.Text, txtNotes.Text, chkDisabled.Checked?1:0, DTPOpenDate.Value, DTPCloseDate.Value);
            sql += string.Format("GETDATE(),'{0}',GETDATE(),'{0}')", FRM.UserName);

            if (SDATA.DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
            {
                SDLL.DLL02.Info("Lưu dữ liệu thành công!");
                (Application.OpenForms["frm0040"] as frm0040).loadDATA();
                btnSave.Enabled = false;
                txtBankAccountID.Enabled = false;
            }
            else
            {
                SDLL.DLL02.Alert("Lưu dữ liệu thất bại!");
            }
        }

        private void UpdateInfo()
        {
            sql = string.Format("UPDATE tb0034 SET AccountID ='{0}',CurrencyID='{1}',BankAccountNo=N'{2}', BankName=N'{3}',",TDBCboAccountID.Text,TDBCboCurrencyID.Text,txtBankAccountNo.Text,txtBankName.Text);
            sql += string.Format("Notes=N'{0}',Disabled='{1}',OpenDate='{2}',CloseDate='{3}',", txtNotes.Text, chkDisabled.Checked?1:0, DTPOpenDate.Value, DTPCloseDate.Value);
            sql += string.Format("LastModifyDate=GETDATE(),LastModifyUserID='{0}' WHERE BankAccountID='{1}'", FRM.UserName, _BankAccountID);

            if (SDATA.DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
            {
                SDLL.DLL02.Info("Cập nhật dữ liệu thành công!");
                (Application.OpenForms["frm0040"] as frm0040).loadDATA();
                Close();
            }
            else
            {
                SDLL.DLL02.Alert("Cập nhật dữ liệu thất bại!");
            }
        }

        private void frm0050_Shown(object sender, EventArgs e)
        {
            loadDATA_Combo();
            if (!_isNew)
            {
                sql = string.Format("SELECT * FROM tb0034 WHERE BankAccountID='{0}'", _BankAccountID);
                DataTable dt = FRM.GetDataTable(sql);
                if (dt.Rows.Count < 1) { return; }
                txtBankAccountID.Text = dt.Rows[0]["BankAccountID"].ToString();
                txtBankAccountNo.Text = dt.Rows[0]["BankAccountNo"].ToString();
                txtBankName.Text = dt.Rows[0]["BankName"].ToString();
                TDBCboAccountID.Text = dt.Rows[0]["AccountID"].ToString();
                TDBCboCurrencyID.Text = dt.Rows[0]["CurrencyID"].ToString();
                txtNotes.Text = dt.Rows[0]["Notes"].ToString();
                DTPOpenDate.Value = DateTime.Parse(dt.Rows[0]["OpenDate"].ToString());
                DTPCloseDate.Value = DateTime.Parse(dt.Rows[0]["CloseDate"].ToString());
                chkDisabled.Checked = dt.Rows[0]["Disabled"].ToString() == "1" ? true : false;
            }
        }

        private void loadDATA_Combo()
        {
            // Tài khoản hạch toán
            sql = "SELECT AccountID,AccountName FROM tb0025 WHERE Disabled=0 And IsNotShow=0 AND GroupID='G01' ORDER BY AccountID";
            TDBCboAccountID.DataSource = FRM.GetDataTable(sql);
            TDBCboAccountID_SelectedIndexChanged(null, null);

            // Loại tiền
            sql = "SELECT * FROM tb0024 WHERE Disabled=0 ORDER BY CurrencyID";
            TDBCboCurrencyID.DataSource = FRM.GetDataTable(sql);
            TDBCboCurrencyID_SelectedIndexChanged(null, null);
        }

        private void TDBCboAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtAccountName.Text = TDBCboAccountID.SelectedItem != null ? TDBCboAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboCurrencyID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtCurrencyName.Text = TDBCboCurrencyID.SelectedItem != null ? TDBCboCurrencyID.SelectedItem.ItemData[1].ToString() : "";
        }



    }
}
